# Based on the Arch package
# Don't building with installed jbigkit package as we build tiff without jbigkit support

_pkgname=tkimg
pkgbase=mingw-w64-${_pkgname}
pkgname=${MINGW_PACKAGE_PREFIX}-$_pkgname
pkgver=2.0.1
pkgrel=1
pkgdesc="Adds support to Tk for many other Image formats: BMP, XBM, XPM, GIF, PNG, JPEG, TIFF and postscript (mingw-64)"
url="https://tkimg.sourceforge.io/"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('BSD')
depends=(#"${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-zlib")
# Uses dtplite for generating the man pages:
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-tcllib"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://downloads.sourceforge.net/project/tkimg/tkimg/${pkgver:0:3}/tkimg%20${pkgver}/Img-${pkgver}.tar.gz"
        002-dtplite_tcl.patch
        003-msys_mingw-no-need-cygpath.patch
        004-aarch64.patch)
sha256sums=('e69d31b3f439a19071e3508a798b9d5dc70b9416e00926cdac12c1c2d50fce83'
            'eb39aa5f628852b8030041c082e1ede89e2516930125572e5b1f8bc1bcbe8fca'
            '68e8b83214ec9523f2be0d144efd3364e4588e769626538d499543504835083f'
            'b9cef10aa71720fb5802518b14763a3144d5db2580f4ef16c147ee054c8ae479')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}"/${_patch}
  done
}

prepare() {
  cd "${srcdir}/Img-${pkgver}"
  
  apply_patch_with_msg \
    002-dtplite_tcl.patch \
    003-msys_mingw-no-need-cygpath.patch \
    004-aarch64.patch

  autoreconf -fiv
}

build() {
  if check_option "debug" "y"; then
    extra_config+=( --enable-symbols )
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  ../Img-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --mandir=${MINGW_PREFIX}/share/man \
    "${extra_config[@]}"
  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make INSTALL_ROOT="${pkgdir}" install-libraries
  make INSTALL_ROOT="${pkgdir}" install-man
  rm -f "${pkgdir}"/${MINGW_PREFIX}/lib/*.sh
}
